package br.uvv.Util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import br.uvv.Model.Anexo;
import br.uvv.Model.Artefato;
import br.uvv.Model.Ator;
import br.uvv.Model.Cenario;
import br.uvv.Model.CenarioBase;
import br.uvv.Model.ClientePortal;
import br.uvv.Model.ClienteProjeto;
import br.uvv.Model.ConfigDescricaoEstendida;
import br.uvv.Model.Contato;
import br.uvv.Model.Dado;
import br.uvv.Model.DescricaoEstendida;
import br.uvv.Model.Documento;
import br.uvv.Model.EntidadeGenerica;
import br.uvv.Model.Fluxo;
import br.uvv.Model.FluxoBase;
import br.uvv.Model.Perfil;
import br.uvv.Model.Projeto;
import br.uvv.Model.Usuario;

public class HibernateUtil {
	
	private static final SessionFactory sessionFactory = buildSessionFactory();
	
	private static SessionFactory buildSessionFactory(){
		try {
			Configuration cfg = getConfiguration();
			return cfg.buildSessionFactory();
		} catch (Throwable e){
			System.out.println("Criação Inicial do SessionFactory falhou. Erro:" + e.getMessage());
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	private static Configuration getConfiguration() {
        Configuration cfg = new Configuration();
        cfg.addAnnotatedClass(Anexo.class);
        cfg.addAnnotatedClass(Artefato.class);
        cfg.addAnnotatedClass(Ator.class);
        cfg.addAnnotatedClass(Cenario.class);
        cfg.addAnnotatedClass(CenarioBase.class);
        cfg.addAnnotatedClass(ClientePortal.class);
        cfg.addAnnotatedClass(ClienteProjeto.class);
        cfg.addAnnotatedClass(ConfigDescricaoEstendida.class);
        cfg.addAnnotatedClass(Contato.class);
        cfg.addAnnotatedClass(Dado.class);
        cfg.addAnnotatedClass(DescricaoEstendida.class);
        cfg.addAnnotatedClass(Documento.class);
        cfg.addAnnotatedClass(EntidadeGenerica.class);
        cfg.addAnnotatedClass(Fluxo.class);
        cfg.addAnnotatedClass(FluxoBase.class);
        cfg.addAnnotatedClass(Perfil.class);
        cfg.addAnnotatedClass(Projeto.class);
        cfg.addAnnotatedClass(Usuario.class);
        
//      cfg.setProperty("hibernate.connection.datasource", "jdbc/tiespec");
        
//		cfg.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
//		cfg.setProperty("hibernate.connection.url", "jdbc:postgresql://localhost:5432/ti_tcc");
//		cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
//        cfg.setProperty("hibernate.connection.username", "geen");
//		cfg.setProperty("hibernate.connection.password", "geen");
		
		cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
		cfg.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/tiespec");
		cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
		cfg.setProperty("hibernate.connection.username", "root");
		cfg.setProperty("hibernate.connection.password", "");
		
        cfg.setProperty("hibernate.current_session_context_class", "thread");
        cfg.setProperty("hibernate.hbm2ddl.auto", "update");
        
        cfg.setProperty("hibernate.show_sql", "true");
        cfg.setProperty("hibernate.format_sql", "true");
        cfg.setProperty("hibernate.generate_statistics", "false");
        cfg.setProperty("hibernate.use_sql_comments", "false");
        
        cfg.setProperty("hibernate.connection.autocommit", "false");
        cfg.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
        cfg.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
       
//      <property name="c3po.min_size">5</property>
//		<property name="c3po.max_size">50</property>
//		<property name="c3po.timeout">300</property>
//		<property name="c3po.max_statements">100</property>
//		<property name="c3po.idle_test_periodo">3000</property>
        
        return cfg;
    }
}
